package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.UpdateInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaUpdate;
import com.meida.module.bus.client.entity.BusScholartyrant;
import com.meida.module.bus.provider.service.BusScholartyrantService;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 后台用户修改详情
 */
@Component
public class AppUserUpdateHandler implements UpdateInterceptor<AppUser> {
    @Autowired
    private BusScholartyrantService busScholartyrantService;
    @Autowired
    private AppUserService appUserService;

    @Override
    public ResultBody validate(CriteriaUpdate cu, EntityMap params) {
        Long userId = params.getLong("userId");
        AppUser user = appUserService.getUserById(userId);
        if (FlymeUtils.isNotEmpty(user)) {
            String email = user.getEmail();
            if (FlymeUtils.isEmpty(email)) {
                return ResultBody.failed("邮箱未设置");
            }
        }
        return ResultBody.ok();
    }

    @Override
    public void complete(CriteriaUpdate cu, EntityMap params, AppUser appUser) {
        BusScholartyrant busScholartyrant = (BusScholartyrant) cu.getEntity(BusScholartyrant.class);
        Long userId = appUser.getUserId();
        busScholartyrant.setScholartyrantId(userId);
        busScholartyrantService.saveOrUpdate(busScholartyrant);

    }

}
